package com.cyt.web.controller.wx;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import com.cyt.wx.model.po.WxAccount;
import com.cyt.wx.model.po.WxApply;
import com.cyt.wx.model.vo.WxAccountVo;
import com.cyt.wx.service.WxAccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 微信模块-公众号管理控制层
 *
 * @author rong yuan
 * @date 2019-8-15 13:39:34
 */
@RestController
public class WxAccountController extends BaseController {

    @Resource
    private WxAccountService wxAccountService;

    /**
     * 统一跳转
     *
     * @param model 数据模型
     * @return Model
     * @author rong yuan
     * @date 2019-8-15 13:39:34
     */
    @RequestMapping("/wx/wxaccount/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 分页查询
     *
     * @param request     请求对象
     * @param wxAccountVo 接受参数对象
     * @param page        当前页面
     * @param limit       页面数量
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-15 13:39:34
     */
    @BaseLog(module = "微信模块", classz = "公众号管理", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxaccount/page", method = RequestMethod.POST)
    public ReturnResult page(HttpServletRequest request, WxAccountVo wxAccountVo, int page, int limit) {
        WxApply wxApply = acceptCurrentApplyId(request);
        if (wxApply != null) {
            wxAccountVo.setApplyId(wxApply.getId());
        }
        return wxAccountService.page(wxAccountVo, page, limit);
    }

    /**
     * list查询
     *
     * @param request     请求对象
     * @param wxAccountVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-15 13:39:34
     */
    @BaseLog(module = "微信模块", classz = "公众号管理", methods = "list查询", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxaccount/list", method = RequestMethod.POST)
    public ReturnResult list(HttpServletRequest request, WxAccountVo wxAccountVo) {
        WxApply wxApply = acceptCurrentApplyId(request);
        if (wxApply != null) {
            wxAccountVo.setApplyId(wxApply.getId());
        }
        return new ReturnResult(wxAccountService.list(wxAccountVo));
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param wxAccountVo 接受参数对象
     * @return ModelAndView
     * @author rong yuan
     * @date 2019-8-15 13:39:34
     */
    @BaseLog(module = "微信模块", classz = "公众号管理", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxaccount/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(WxAccountVo wxAccountVo) {
        ModelAndView view = new ModelAndView("/wx/wxaccount/wxAccountEdit");
        WxAccount wxAccount;
        if (StringUtils.isNotBlank(wxAccountVo.getId())) {
            wxAccount = wxAccountService.selectByPrimaryKey(wxAccountVo.getId());
        } else {
            wxAccount = new WxAccount();
            wxAccount.setAccountType(wxAccountVo.getAccountType());
        }
        view.addObject(wxAccount);
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request     请求对象
     * @param wxAccountVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-15 13:39:34
     */
    @BaseLog(module = "微信模块", classz = "公众号管理", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxaccount/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, WxAccountVo wxAccountVo) {
        WxApply wxApply = acceptCurrentApplyId(request);
        if (wxApply != null) {
            wxAccountVo.setApplyId(wxApply.getId());
        }
        return wxAccountService.addOrUpdate(wxAccountVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request     请求对象
     * @param wxAccountVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-15 13:39:34
     */
    @BaseLog(module = "微信模块", classz = "公众号管理", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxaccount/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, WxAccountVo wxAccountVo) {
        return wxAccountService.remove(wxAccountVo, acceptCurrentUser(request));
    }
}